package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 分类业务层
 */
@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品和对应的口味
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void save(DishDTO dishDTO) {
        //1.组装Dish
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        if(dish.getStatus() == null){
            dish.setStatus(StatusConstant.ENABLE);
        }
        //2.调用mapper保存dish
        dishMapper.insert(dish);
        //3.组装DishFlavor集合
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(f -> f.setDishId(dish.getId()));
        //4.调用mapper保存dishFlavors
        dishFlavorMapper.insertBatch(flavors);
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 菜品批量删除
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //1.先判断被删除的菜品中是否有菜品为启售状态，如果有，不能删除
        Integer count = dishMapper.countEnablesByIds(ids);
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        //2.再判断被删除的菜品中有没有菜品被套餐关联，如果有，不能删除
        count = setmealMapper.countByDishIds(ids);
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //3.删除菜品
        dishMapper.deleteBatch(ids);

        //4.删除菜品关联的口味数据
        dishFlavorMapper.deleteBatch(ids);
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public DishVO getById(Long id) {
        //1.根据id查询菜品
        DishVO dishVo = dishMapper.getById(id);

        //2.根据id查询菜品口味
//        List<DishFlavor> dishFlavors = dishFlavorMapper.queryByDishId(id);

        //3.组装dishVO并返回
//        dishVo.setFlavors(dishFlavors);
        return dishVo;
    }


    /**
     * 根据分类id查询菜品
     */
    @Override
    public List<Dish> getByCategoryId(Long categoryId) {

        return dishMapper.getByCategoryId(categoryId);
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     * @return
     */
    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        //1.修改主表菜品数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        //2.删除从表菜品口味数据
        List<Long> ids = new ArrayList<>();
        ids.add(dish.getId());
        dishFlavorMapper.deleteBatch(ids);
        //3.批量新增从表菜品口味数据
        dishDTO.getFlavors().forEach(f -> f.setDishId(dish.getId()));
        dishFlavorMapper.insertBatch(dishDTO.getFlavors());

    }

    /**
     * 启售停售状态
     *
     * @param
     * @return
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())
                .build();
        dishMapper.update(dish);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> list(Dish dish) {

        return dishMapper.list(dish);
    }
}
